package com.zhouwei.mzbanner;

import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import com.zhouwei.mzbanner.utils.AttrUtils;
import com.zhouwei.mzbanner.utils.DensityUtils;
import com.zhouwei.mzbanner.utils.LogUtil;
import ohos.agp.components.*;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouwei on 17/5/26.
 */

public class MZBannerView<T> extends DependentLayout implements Component.TouchEventListener {
    private static final String TAG = "MZBannerView";
    private CustomViewPager mViewPager;
    private MZPagerAdapter mAdapter;
    private List<T> mDatas;
    private boolean mIsAutoPlay = true;// 是否自动播放
    private int mCurrentItem = 0;//当前位置
    private EventHandler mHandler = new EventHandler(EventRunner.create());
    private int mDelayedTime = 3000;// Banner 切换时间间隔
    //    private ViewPagerScroller mViewPagerScroller;//控制ViewPager滑动速度的Scroller
    private boolean mIsOpenMZEffect = true;// 开启魅族Banner效果
    private boolean mIsCanLoop = true;// 是否轮播图片
    private DirectionalLayout mIndicatorContainer;//indicator容器
    private ArrayList<Image> mIndicators = new ArrayList<>();
    //mIndicatorRes[0] 为为选中，mIndicatorRes[1]为选中
    private int[] mIndicatorRes = new int[]{ResourceTable.Media_indicator_normal, ResourceTable.Media_indicator_selected};
    private int mIndicatorPaddingLeft = 0;// indicator 距离左边的距离
    private int mIndicatorPaddingRight = 0;//indicator 距离右边的距离
    private int mIndicatorPaddingTop = 0;//indicator 距离上边的距离
    private int mIndicatorPaddingBottom = 0;//indicator 距离下边的距离
    private int mMZModePadding = 0;//在仿魅族模式下，由于前后显示了上下一个页面的部分，因此需要计算这部分padding
    private int mIndicatorAlign = 1;
    private PageSlider.PageChangedListener mOnPageChangeListener;
    private BannerPageClickListener mBannerPageClickListener;
    /**
     * 中间Page是否覆盖两边，默认覆盖
     */
    private boolean mIsMiddlePageCover = true;

    private static final String open_mz_mode = "open_mz_mode";
    private static final String middle_page_cover = "middle_page_cover";
    private static final String canLoop = "canLoop";
    private static final String indicatorAlign = "indicatorAlign";
    private static final String indicatorPaddingLeft = "indicatorPaddingLeft";
    private static final String indicatorPaddingRight = "indicatorPaddingRight";
    private static final String indicatorPaddingTop = "indicatorPaddingTop";
    private static final String indicatorPaddingBottom = "indicatorPaddingBottom";

    public enum IndicatorAlign {
        LEFT,//做对齐
        CENTER,//居中对齐
        RIGHT //右对齐
    }


    public MZBannerView(Context context) {
        super(context);
        init();
    }

    public MZBannerView(Context context, AttrSet attrs) {
        super(context, attrs);
        readAttrs(attrs);
        init();
    }

    public MZBannerView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        readAttrs(attrs);
        init();
    }

    private void readAttrs(AttrSet attrs) {
        mIsOpenMZEffect = AttrUtils.getBooleanFromAttr(attrs, open_mz_mode, true);
        mIsMiddlePageCover = AttrUtils.getBooleanFromAttr(attrs, middle_page_cover, true);
        mIsCanLoop = AttrUtils.getBooleanFromAttr(attrs, canLoop, true);
        mIndicatorAlign = IndicatorAlign.CENTER.ordinal();
        mIndicatorPaddingLeft = AttrUtils.getDimensionFromAttr(attrs, indicatorPaddingLeft, 0);
        mIndicatorPaddingRight = AttrUtils.getDimensionFromAttr(attrs, indicatorPaddingRight, 0);
        mIndicatorPaddingTop = AttrUtils.getDimensionFromAttr(attrs, indicatorPaddingTop, 0);
        mIndicatorPaddingBottom = AttrUtils.getDimensionFromAttr(attrs, indicatorPaddingBottom, 0);
        String indicatorAlignValue = AttrUtils.getStringFromAttr(attrs, indicatorAlign, IndicatorAlign.CENTER.name());
        if (indicatorAlignValue != null && indicatorAlignValue.length() > 0) {
            switch (indicatorAlignValue) {
                case "left":
                    mIndicatorAlign = IndicatorAlign.LEFT.ordinal();
                    break;
                case "center":
                    mIndicatorAlign = IndicatorAlign.CENTER.ordinal();
                    break;
                case "right":
                    mIndicatorAlign = IndicatorAlign.RIGHT.ordinal();
                    break;
            }
        }
        LogUtil.error(TAG, "1====readAttrs====" + mIsOpenMZEffect + "," + mIsMiddlePageCover + "," + mIsCanLoop + "," + mIndicatorPaddingTop + "," + mIndicatorPaddingBottom);
    }


    private void init() {
        Component view = null;
        if (mIsOpenMZEffect) {
            view = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_mz_banner_effect_layout, this, true);
        } else {
            view = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_mz_banner_normal_layout, this, true);
        }
        mIndicatorContainer = (DirectionalLayout) view.findComponentById(ResourceTable.Id_banner_indicator_container);
        mViewPager = (CustomViewPager) view.findComponentById(ResourceTable.Id_mzbanner_vp);
//      mViewPager.setOffscreenPageLimit(4);
//        mViewPager.setPageCacheSize(4);

        mMZModePadding = DensityUtils.dpToPx(30, getContext());
        // 初始化Scroller
        initViewPagerScroll();

        sureIndicatorPosition();
        setTouchEventListener(this);
    }

    /**
     * 是否开启魅族模式
     */
    private void setOpenMZEffect() {
        // 魅族模式
        if (mIsOpenMZEffect) {
            mViewPager.setClipEnabled(true);
        }
    }

    /**
     * make sure the indicator
     */
    private void sureIndicatorPosition() {
        if (mIndicatorAlign == IndicatorAlign.LEFT.ordinal()) {
            setIndicatorAlign(IndicatorAlign.LEFT);
        } else if (mIndicatorAlign == IndicatorAlign.CENTER.ordinal()) {
            setIndicatorAlign(IndicatorAlign.CENTER);
        } else {
            setIndicatorAlign(IndicatorAlign.RIGHT);
        }
    }

    /**
     * 设置ViewPager的滑动速度
     */
    private void initViewPagerScroll() {
//        try {
//            Field mScroller = null;
//            mScroller = ViewPager.class.getDeclaredField("mScroller");
//            mScroller.setAccessible(true);
//            mViewPagerScroller = new ViewPagerScroller(
//                 mViewPager.getContext());
//            mScroller.set(mViewPager, mViewPagerScroller);
//
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
    }


    private final Runnable mLoopRunnable = new Runnable() {
        @Override
        public void run() {
            LogUtil.error(this.getClass().getName(), "1=======mLoopRunnable 55555===" + mAdapter.getCount() + "," + mIsAutoPlay);
            if (mIsAutoPlay) {
                mCurrentItem = mViewPager.getCurrentPage();
                LogUtil.error(this.getClass().getName(), "1=======mLoopRunnable START===" + mCurrentItem);
                mCurrentItem++;
                LogUtil.error(this.getClass().getName(), "1=======mLoopRunnable 0===" + mAdapter.getCount());
                if (mCurrentItem == mAdapter.getCount() - 1) {
                    LogUtil.error(this.getClass().getName(), "1=======mLoopRunnable 1===" + mCurrentItem);
                    mCurrentItem = 0;
                    getContext().getUITaskDispatcher().asyncDispatch(() -> mViewPager.setCurrentPage(mCurrentItem, false));
                    mHandler.postTask(mLoopRunnable, mDelayedTime);
                } else {
                    LogUtil.error(this.getClass().getName(), "1=======mLoopRunnable 2===" + mCurrentItem);
                    getContext().getUITaskDispatcher().asyncDispatch(() -> mViewPager.setCurrentPage(mCurrentItem));
                    mHandler.postTask(mLoopRunnable, mDelayedTime);
                }
            } else {
                mHandler.postTask(mLoopRunnable, mDelayedTime);
            }
        }
    };


    /**
     * 初始化指示器Indicator
     */
    private void initIndicator() {
        mIndicatorContainer.removeAllComponents();
        mIndicators.clear();
        for (int i = 0; i < mDatas.size(); i++) {
            Image imageView = new Image(getContext());
            if (mIndicatorAlign == IndicatorAlign.LEFT.ordinal()) {
                if (i == 0) {
                    int paddingLeft = mIsOpenMZEffect ? mIndicatorPaddingLeft + mMZModePadding : mIndicatorPaddingLeft;
                    imageView.setPadding(paddingLeft + 6, 0, 6, 0);
                } else {
                    imageView.setPadding(6, 0, 6, 0);
                }

            } else if (mIndicatorAlign == IndicatorAlign.RIGHT.ordinal()) {
                if (i == mDatas.size() - 1) {
                    int paddingRight = mIsOpenMZEffect ? mMZModePadding + mIndicatorPaddingRight : mIndicatorPaddingRight;
                    imageView.setPadding(6, 0, 6 + paddingRight, 0);
                } else {
                    imageView.setPadding(6, 0, 6, 0);
                }

            } else {
                imageView.setPadding(6, 0, 6, 0);
            }

            if (i == (mCurrentItem % mDatas.size())) {
                imageView.setImageAndDecodeBounds((mIndicatorRes[1]));
            } else {
                imageView.setImageAndDecodeBounds((mIndicatorRes[0]));
            }
            mIndicators.add(imageView);
            LogUtil.error(this.getName(), "1=====mIndicatorContainer===" + mIndicatorContainer);
            mIndicatorContainer.addComponent(imageView);
        }
    }


    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (!mIsCanLoop) {
            return false;
        }
        switch (touchEvent.getAction()) {
            // 按住Banner的时候，停止自动轮播
            case TouchEvent.POINT_MOVE:
            case TouchEvent.CANCEL:
            case TouchEvent.PRIMARY_POINT_DOWN:
                int paddingLeft = mViewPager.getLeft();
                float touchX = touchEvent.getPointerScreenPosition(0).getX();
                // 如果是魅族模式，去除两边的区域
                if (touchX >= paddingLeft && touchX < getScreenWidth(getContext()) - paddingLeft) {
                    pause();
                }
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                LogUtil.error(this.getClass().getName(), "1=======PRIMARY_POINT_UP===");
                startSlide();
                break;
        }
        return false;
    }

    public static int getScreenWidth(Context context) {
        int width = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().width;
        return width;
    }

    private void startSlide() {
        // 如果Adapter为null, 说明还没有设置数据，这个时候不应该轮播Banner
        if (mAdapter == null) {
            return;
        }
        LogUtil.error(this.getClass().getName(), "1=======PRIMARY_POINT_UP===mIsCanLoop===" + mIsCanLoop);

        if (mIsCanLoop) {
            pause();
            mIsAutoPlay = true;
            mHandler.postTask(mLoopRunnable);
        }
    }

    /******************************************************************************************************/
    /**                             对外API                                                               **/
    /******************************************************************************************************/
    /**
     * 开始轮播
     * <p>应该确保在调用用了{@link MZBannerView {@link #setPages(List, MZHolderCreator)}} 之后调用这个方法开始轮播</p>
     */
    public void start() {
        // 如果Adapter为null, 说明还没有设置数据，这个时候不应该轮播Banner
        if (mAdapter == null) {
            return;
        }
        LogUtil.error(this.getClass().getName(), "1=======PRIMARY_POINT_UP===mIsCanLoop===" + mIsCanLoop);

        if (mIsCanLoop) {
            pause();
            mIsAutoPlay = true;
            mHandler.postTask(mLoopRunnable, mDelayedTime);
        }
    }


    /**
     * 停止轮播
     */
    public void pause() {
        mIsAutoPlay = false;
        mHandler.removeTask(mLoopRunnable);
    }

    /**
     * 设置是否可以轮播
     *
     * @param canLoop
     */
    public void setCanLoop(boolean canLoop) {
        mIsCanLoop = canLoop;
        if (!canLoop) {
            pause();
        }
    }

    /**
     * 设置BannerView 的切换时间间隔
     *
     * @param delayedTime
     */
    public void setDelayedTime(int delayedTime) {
        mDelayedTime = delayedTime;
    }

    public void addPageChangeListener(PageSlider.PageChangedListener onPageChangeListener) {
        mOnPageChangeListener = onPageChangeListener;
    }

    /**
     * 添加Page点击事件
     *
     * @param bannerPageClickListener {@link BannerPageClickListener}
     */
    public void setBannerPageClickListener(BannerPageClickListener bannerPageClickListener) {
        mBannerPageClickListener = bannerPageClickListener;
    }

    /**
     * 是否显示Indicator
     *
     * @param visible true 显示Indicator，否则不显示
     */
    public void setIndicatorVisible(boolean visible) {
        if (visible) {
            mIndicatorContainer.setVisibility(VISIBLE);
        } else {
            mIndicatorContainer.setVisibility(HIDE);
        }
    }

    /**
     * set indicator padding
     *
     * @param paddingLeft
     * @param paddingTop
     * @param paddingRight
     * @param paddingBottom
     */
    public void setIndicatorPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
        mIndicatorPaddingLeft = paddingLeft;
        mIndicatorPaddingTop = paddingTop;
        mIndicatorPaddingRight = paddingRight;
        mIndicatorPaddingBottom = paddingBottom;
        sureIndicatorPosition();
    }


    public PageSlider getViewPager() {
        return mViewPager;
    }

    /**
     * 设置indicator 图片资源
     *
     * @param unSelectRes 未选中状态资源图片
     * @param selectRes   选中状态资源图片
     */
    public void setIndicatorRes(int unSelectRes, int selectRes) {
        mIndicatorRes[0] = unSelectRes;
        mIndicatorRes[1] = selectRes;
    }

    /**
     * 设置数据，这是最重要的一个方法。
     * <p>其他的配置应该在这个方法之前调用</p>
     *
     * @param datas           Banner 展示的数据集合
     * @param mzHolderCreator ViewHolder生成器 {@link MZHolderCreator} And {@link MZViewHolder}
     */
    public void setPages(List<T> datas, MZHolderCreator mzHolderCreator) {
        if (datas == null || mzHolderCreator == null) {
            return;
        }
        mDatas = datas;
        //如果在播放，就先让播放停止
        pause();

        //增加一个逻辑：由于魅族模式会在一个页面展示前后页面的部分，因此，数据集合的长度至少为3,否则，自动为普通Banner模式
        //不管配置的:open_mz_mode 属性的值是否为true

        if (datas.size() < 3) {
            mIsOpenMZEffect = false;
            ComponentContainer.LayoutConfig layoutParams = mViewPager.getLayoutConfig();
            layoutParams.setMargins(0, 0, 0, 0);
            mViewPager.setLayoutConfig(layoutParams);
            mViewPager.setClipEnabled(true);
        }
        setOpenMZEffect();
        // 2017.7.20 fix：将Indicator初始化放在Adapter的初始化之前，解决更新数据变化更新时crush.
        //初始化Indicator
        initIndicator();

        mAdapter = new MZPagerAdapter(datas, mzHolderCreator, mIsCanLoop);
        mAdapter.setUpViewViewPager(mViewPager);
        mViewPager.setProvider(mAdapter);
        mAdapter.notifyDataChanged();
        //TODO 初始化立即左移最后一个索引
        if (mIsOpenMZEffect && datas != null && !datas.isEmpty()) {
            mViewPager.setCurrentPage(datas.size());
        }
        mAdapter.setPageClickListener(mBannerPageClickListener);
        mViewPager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
                int realPosition = position % mIndicators.size();
                if (mOnPageChangeListener != null) {
                    mOnPageChangeListener.onPageSliding(realPosition, positionOffset, positionOffsetPixels);
                }
            }

            @Override
            public void onPageSlideStateChanged(int state) {
                switch (state) {
                    case PageSlider.SLIDING_STATE_DRAGGING:
                        mIsAutoPlay = false;
                        break;
                    case PageSlider.SLIDING_STATE_SETTLING:
                        mIsAutoPlay = true;
                        break;

                }
                if (mOnPageChangeListener != null) {
                    mOnPageChangeListener.onPageSlideStateChanged(state);
                }
                LogUtil.error(this.getClass().getName(), "1=======onPageSlideStateChanged====" + state);
            }

            @Override
            public void onPageChosen(int position) {
                mCurrentItem = position;

                // 切换indicator
                int realSelectPosition = mCurrentItem % mIndicators.size();
                for (int i = 0; i < mDatas.size(); i++) {
                    if (i == realSelectPosition) {
                        mIndicators.get(i).setImageAndDecodeBounds(mIndicatorRes[1]);
                    } else {
                        mIndicators.get(i).setImageAndDecodeBounds(mIndicatorRes[0]);
                    }
                }
                // 不能直接将mOnPageChangeListener 设置给ViewPager ,否则拿到的position 是原始的position
                if (mOnPageChangeListener != null) {
                    mOnPageChangeListener.onPageChosen(realSelectPosition);
                }
            }
        });
    }

    /**
     * 设置Indicator 的对齐方式
     *
     * @param indicatorAlign {@link IndicatorAlign#CENTER }{@link IndicatorAlign#LEFT }{@link IndicatorAlign#RIGHT }
     */
    public void setIndicatorAlign(IndicatorAlign indicatorAlign) {
        mIndicatorAlign = indicatorAlign.ordinal();
//        DependentLayout.LayoutConfig layoutParams = (DependentLayout.LayoutConfig) mIndicatorContainer.getLayoutConfig();
//        if (indicatorAlign == IndicatorAlign.LEFT) {
//            layoutParams.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT);
//        } else if (indicatorAlign == IndicatorAlign.RIGHT) {
//            layoutParams.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT);
//        } else {
//            layoutParams.addRule(LayoutConfig.HORIZONTAL_CENTER);
//        }
//
//        // 2017.8.27 添加：增加设置Indicator 的上下边距。
//
//        layoutParams.setMargins(0, mIndicatorPaddingTop, 0, mIndicatorPaddingBottom);
//        mIndicatorContainer.setLayoutConfig(layoutParams);

    }


    public DirectionalLayout getIndicatorContainer() {
        return mIndicatorContainer;
    }

    /**
     * 设置ViewPager切换的速度
     *
     * @param duration 切换动画时间
     */
    public void setDuration(int duration) {
        mViewPager.setPageSwitchTime(duration);
    }

//    /**
//     * 设置是否使用ViewPager默认是的切换速度
//     *
//     * @param useDefaultDuration 切换动画时间
//     */
//    public void setUseDefaultDuration(boolean useDefaultDuration) {
//        mViewPagerScroller.setUseDefaultDuration(useDefaultDuration);
//    }


    public int getDuration() {
        return mViewPager.getPageSwitchTime();
    }


    public static class MZPagerAdapter<T> extends PageSliderProvider {
        private List<T> mDatas;
        private MZHolderCreator mMZHolderCreator;
        private PageSlider mViewPager;
        private boolean canLoop;
        private BannerPageClickListener mPageClickListener;
        private final int mLooperCountFactor = 500;

        public MZPagerAdapter(List<T> datas, MZHolderCreator MZHolderCreator, boolean canLoop) {
            mDatas = new ArrayList<>();
            //mDatas.add(datas.get(datas.size()-1));// 加入最后一个
            for (T t : datas) {
                mDatas.add(t);
            }
            LogUtil.error(this.toString(), "1=======MZPagerAdapter=====" + mDatas.size());
            // mDatas.add(datas.get(0));//在最后加入最前面一个
            mMZHolderCreator = MZHolderCreator;
            this.canLoop = canLoop;
        }

        public void setPageClickListener(BannerPageClickListener pageClickListener) {
            mPageClickListener = pageClickListener;
        }

        /**
         * 初始化Adapter和设置当前选中的Item
         *
         * @param viewPager
         */
        public void setUpViewViewPager(CustomViewPager viewPager) {
            mViewPager = viewPager;
//            int currentItem = canLoop ? getStartSelectItem() : 0;
//            LogUtil.error(this.getClass().getName(), "1=======setUpViewViewPager===" + currentItem);
//
//            //设置当前选中的Item
//            mViewPager.setCurrentPage(currentItem);
        }

        private int getStartSelectItem() {
            if (getRealCount() == 0) {
                return 0;
            }
            // 我们设置当前选中的位置为Integer.MAX_VALUE / 2,这样开始就能往左滑动
            // 但是要保证这个值与getRealPosition 的 余数为0，因为要从第一页开始显示
            int currentItem = getRealCount() * mLooperCountFactor / 2;
            if (currentItem % getRealCount() == 0) {
                return currentItem;
            }
            // 直到找到从0开始的位置
            while (currentItem % getRealCount() != 0) {
                currentItem++;
            }
            return currentItem;
        }

        public void setDatas(List<T> datas) {
            mDatas = datas;
        }

        @Override
        public int getCount() {
            // 2017.6.10 bug fix
            // 如果getCount 的返回值为Integer.MAX_VALUE 的话，那么在setCurrentItem的时候会ANR(除了在onCreate 调用之外)
            LogUtil.error("MABannerView", "1=======getCount===" + getRealCount());
//            return getRealCount();//ViewPager返回int 最大值
            //TODO 循环串起来轮播
            return canLoop ? getRealCount() * mLooperCountFactor : getRealCount();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int position) {
            LogUtil.error("MABannerView", "1=======createPageInContainer===" + componentContainer + "," + position);
            Component view = getView(position, componentContainer);
            if (componentContainer != null && view != null) {
                componentContainer.addComponent(view);
            }
            return view;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object object) {
            if (componentContainer != null) {
                componentContainer.getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        componentContainer.removeComponent((Component) object);
                    }
                });
            }
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return component == o;
        }

        @Override
        public void onUpdateFinished(ComponentContainer container) {
            if (canLoop) {
                int position = mViewPager.getCurrentPage();
                if (position == getCount() - 1) {
                    position = 0;
                    setCurrentItem(position);
                }
            }
        }

        private void setCurrentItem(int position) {
            try {
                mViewPager.setCurrentPage(position, false);
            } catch (IllegalStateException e) {
                LogUtil.error(this.getClass().getName(), "setCurrentItem--" + e);
            }
        }


        private int getRealCount() {
            return mDatas == null ? 0 : mDatas.size();
        }


        private Component getView(int position, ComponentContainer container) {

            final int realPosition = position % getRealCount();
            MZViewHolder holder = null;
            // create holder
            holder = mMZHolderCreator.createViewHolder();

            if (holder == null) {
                throw new RuntimeException("can not return a null holder");
            }

            if (container == null) {
                return null;
            }
            // create View
            Component view = holder.createView(container.getContext());

            if (mDatas != null && mDatas.size() > 0) {
                holder.onBind(container.getContext(), realPosition, mDatas.get(realPosition));
            }

            // 添加点击事件
            view.setClickedListener(new ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (mPageClickListener != null) {
                        mPageClickListener.onPageClick(component, realPosition);
                    }
                }
            });

            return view;
        }


    }

//    /**
//     * ＊由于ViewPager 默认的切换速度有点快，因此用一个Scroller 来控制切换的速度
//     * <p>而实际上ViewPager 切换本来就是用的Scroller来做的，因此我们可以通过反射来</p>
//     * <p>获取取到ViewPager 的 mScroller 属性，然后替换成我们自己的Scroller</p>
//     */
//    public static class ViewPagerScroller extends ScrollHelper {
//        private int mDuration = 800;// ViewPager默认的最大Duration 为600,我们默认稍微大一点。值越大越慢。
//        private boolean mIsUseDefaultDuration = false;
//
//
////        @Override
////        public void startScroll(int startX, int startY, int dx, int dy) {
////            super.startScroll(startX, startY, dx, dy, mDuration);
////        }
////
////
////
////        @Override
////        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
////            super.startScroll(startX, startY, dx, dy, mIsUseDefaultDuration ? duration : mDuration);
////        }
//
//
//        public void setUseDefaultDuration(boolean useDefaultDuration) {
//            mIsUseDefaultDuration = useDefaultDuration;
//        }
//
//        public boolean isUseDefaultDuration() {
//            return mIsUseDefaultDuration;
//        }
//
//        public void setDuration(int duration) {
//            mDuration = duration;
//        }
//
//
//        public int getScrollDuration() {
//            return mDuration;
//        }
//    }

    /**
     * Banner page 点击回调
     */
    public interface BannerPageClickListener {
        void onPageClick(Component view, int position);
    }


}
